﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class tipoafiliacionController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /tipoafiliacion/

        public ActionResult Index()
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            

            var query = entidades.TIPOAFILIACIONs;
            
            var result = (from eps in query.OrderBy(ta => ta.NomTipoAfiliacion).Skip(start).Take(limit).ToList()
                          
                          select new
                          {
                              Id = eps.IdTipoAfiliacion,
                              Nombre = eps.NomTipoAfiliacion,
                              
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);

        }
        public ActionResult Index2()
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);


            string tipoafiliacion = Convert.ToString(Request.QueryString["query"]);
            if (String.IsNullOrEmpty(tipoafiliacion)) { tipoafiliacion = " "; }

            var query = entidades.TIPOAFILIACIONs.Where(p => (p.NomTipoAfiliacion.Trim() + " ").ToUpper().Contains(tipoafiliacion.ToUpper())); ;


            var result = (from eps in query.OrderBy(ta => ta.NomTipoAfiliacion).Skip(start).Take(limit).ToList()

                          select new
                          {
                              IdTipoAfiliacion = eps.IdTipoAfiliacion,
                              NomTipoAfiliacion = eps.NomTipoAfiliacion,


                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);

        }

        //
        // GET: /tipoafiliacion/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /tipoafiliacion/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /tipoafiliacion/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection, TIPOAFILIACION tipoafiliacion)
        {
            
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {

                    var tia = new TIPOAFILIACION();
                    TryUpdateModel(tia, collection.ToValueProvider());
                    entidades.AddToTIPOAFILIACIONs(tia);
                    entidades.SaveChanges();
                    return Content("OK");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /tipoafiliacion/Edit/5
 
        public ActionResult Edit(int id)
        {
            var ti = entidades.TIPOAFILIACIONs.Single(tia => tia.IdTipoAfiliacion == id);
            return View(ti);
        }

        //
        // POST: /tipoafiliacion/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection, TIPOAFILIACION TIPOAFILIACION)
        {
            var ta = entidades.TIPOAFILIACIONs.Single(tia => tia.IdTipoAfiliacion == id);
            try
            {
                
                if (ModelState.IsValid)
                {

                    TryUpdateModel(ta);
                    entidades.SaveChanges();
                    return Content("OK");
                }
                return View(ta);
            }
            catch
            {
                return View(ta);
            }
        }

        //
        // GET: /tipoafiliacion/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /tipoafiliacion/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
